from django.db import models
from django.contrib.auth.models import AbstractUser


# Create your models here.

class User(AbstractUser):
    """
        自定义用户模型类
    """
    nickname = models.CharField(max_length=100, null=False, verbose_name='昵称')
    phone = models.CharField(max_length=11, unique=True, verbose_name='手机号')
    credit = models.IntegerField(verbose_name="信用积分", null=False, default=100)

    UNSURE = 0
    VISITOR = 1
    TOUR_CORP = 2
    HOTEL = 3
    ADMINISTRATOR = 4
    IDENTITY_CHOICES = (
        (UNSURE, '待完善'),
        (VISITOR, '旅客'),
        (TOUR_CORP, '旅游公司'),
        (HOTEL, '宾馆'),
        (ADMINISTRATOR, '管理员'),
    )
    identity = models.PositiveSmallIntegerField(verbose_name='用户类型', choices=IDENTITY_CHOICES, default=UNSURE)

    class Meta:
        db_table = 'user'
        verbose_name = verbose_name_plural = '用户'

    def __str__(self):
        return self.username


class Application(models.Model):
    """
        特殊账号注册申请模型
    """
    applicant = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='申请者', related_name='my_application')
    # 申请注册的账号类型
    identity = models.SmallIntegerField(verbose_name="账号类型")

    PENDING = 0
    SUCCESS = 1
    FAILURE = 2
    STATE_CHOICES = [
        (PENDING, '待受理'),
        (SUCCESS, '成功'),
        (FAILURE, '失败'),
    ]
    state = models.SmallIntegerField(verbose_name='状态', choices=STATE_CHOICES, default=PENDING)

    class Meta:
        db_table = 'application'
        verbose_name = verbose_name_plural = '注册申请'

    def __str__(self):
        return str(self.identity) + ', ' + self.applicant.username
